package com.agger.eurekaclientuserservice.services.impl;

import com.agger.eurekaclientuserservice.mappers.UserMapper;
import com.agger.eurekaclientuserservice.services.UserService;
import com.agger.eurekacommonapi.vo.PageResult;
import com.agger.eurekacommonapi.vo.UserVO;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.List;

/**
 * @classname: UserServiceImpl
 * @description: 实现类
 * @author chenhx
 * @date 2020-07-16 16:18:31
 */
@Service
public class UserServiceImpl implements UserService {

    @Resource
    private UserMapper mapper;

    @Override
    public int addUser(UserVO param) {
        return mapper.addUser(param);
    }

    @Override
    public int updateUser(UserVO param) {
        return mapper.updateUser(param);
    }

    @Override
    public int deleteUser(UserVO param) {
        return mapper.deleteUser(param);
    }

    /**
     * @Title: selectUserList
     * @Description: 查询用户列表（使用PageHelper分页 普通用法）
     * @param
     * @return com.agger.eurekacommonapi.vo.PageResult<com.agger.eurekacommonapi.vo.UserVO>
     * @author chenhx
     * @date 2020-07-20 16:10:58
     */
    @Override
    public PageResult<UserVO> selectUserList(UserVO param) {

        // 传入分页
        PageHelper.startPage(param.getPageNum(), param.getPageSize());
        // 查询出结果
        List<UserVO> list = mapper.selectUserList(param);
        // 组装pageInfo对象
        PageInfo<UserVO> pageInfo = new PageInfo<>(list);
        // 封装结果
        PageResult<UserVO> result = new PageResult<UserVO>().setTotal(pageInfo.getTotal()).setRows(pageInfo.getList());
        return result;
    }

    /**
     * @Title: selectUserAccount
     * @Description: 查询用户账号列表（使用PageHelper分页 lambda用法）
     * @param
     * @return com.agger.eurekacommonapi.vo.PageResult<java.lang.String>
     * @author chenhx
     * @date 2020-07-20 16:10:54
     */
    @Override
    public PageResult<String> selectUserAccount(UserVO param) {

        // 传入分页，并执行查询，返回分页对象信息
        PageInfo<String> pageInfo = PageHelper.startPage(param.getPageNum(), param.getPageSize()).doSelectPageInfo(() ->
                mapper.selectUserAccountList(param));
        // 封装结果
        PageResult<String> result = new PageResult<String>().setTotal(pageInfo.getTotal()).setRows(pageInfo.getList());
        return result;
    }
}
